<?php


namespace app\api\unionpay\ysf;

class Request
{
	private $resHandler;
	private $reqHandler;
	private $pay;
	private $cfg;
	public function __construct($config)
	{
		$this->resHandler = new ClientResponseHandler();
		$this->reqHandler = new RequestHandler();
		$this->pay = new PayHttpClient();
		$this->cfg = $config;
		$this->reqHandler->setGateUrl($this->cfg->C("url"));
		$sign_type = $this->cfg->C("sign_type");
		if ($sign_type == "MD5") {
			$this->reqHandler->setKey($this->cfg->C("key"));
			$this->resHandler->setKey($this->cfg->C("key"));
			$this->reqHandler->setSignType($sign_type);
		} else {
			if ($sign_type == "RSA_1_1" || $sign_type == "RSA_1_256") {
				$this->reqHandler->setRSAKey($this->cfg->C("private_rsa_key"));
				$this->resHandler->setRSAKey($this->cfg->C("public_rsa_key"));
				$this->reqHandler->setSignType($sign_type);
			}
		}
	}
	public function submitOrderInfo($param, $notify_url)
	{
		$this->reqHandler->setReqParams($param, ["method"]);
		$this->reqHandler->setParameter("service", "unified.trade.native");
		$this->reqHandler->setParameter("mch_id", $this->cfg->C("mchId"));
		$this->reqHandler->setParameter("version", $this->cfg->C("version"));
		$this->reqHandler->setParameter("sign_type", $this->cfg->C("sign_type"));
		$this->reqHandler->setParameter("notify_url", $notify_url);
		$this->reqHandler->setParameter("nonce_str", mt_rand());
		$this->reqHandler->createSign();
		$data = Utils::toXml($this->reqHandler->getAllParameters());
		$this->pay->setReqContent($this->reqHandler->getGateURL(), $data);
		if ($this->pay->call()) {
			$this->resHandler->setContent($this->pay->getResContent());
			$this->resHandler->setKey($this->reqHandler->getKey());
			if ($this->resHandler->isTenpaySign()) {
				if ($this->resHandler->getParameter("status") == 0 && $this->resHandler->getParameter("result_code") == 0) {
					return ["code_img_url" => $this->resHandler->getParameter("code_img_url"), "code_url" => $this->resHandler->getParameter("code_url"), "code_status" => $this->resHandler->getParameter("code_status"), "type" => $this->reqHandler->getParameter("service")];
				} else {
					return "Error Code:" . $this->resHandler->getParameter("err_code") . " Error Message:" . $this->resHandler->getParameter("err_msg");
				}
			}
			return "Error Code:" . $this->resHandler->getParameter("status") . " Error Message:" . $this->resHandler->getParameter("message");
		} else {
			return "Response Code:" . $this->pay->getResponseCode() . " Error Info:" . $this->pay->getErrInfo();
		}
	}
	public function queryOrder()
	{
		$this->reqHandler->setReqParams($_POST, ["method"]);
		$reqParam = $this->reqHandler->getAllParameters();
		if (empty($reqParam["transaction_id"]) && empty($reqParam["out_trade_no"])) {
			echo json_encode(["status" => 500, "msg" => "请输入商户订单号,平台订单号!"]);
			exit;
		}
		$this->reqHandler->setParameter("version", $this->cfg->C("version"));
		$this->reqHandler->setParameter("service", "unified.trade.query");
		$this->reqHandler->setParameter("mch_id", $this->cfg->C("mchId"));
		$this->reqHandler->setParameter("nonce_str", mt_rand());
		$this->reqHandler->setParameter("sign_type", $this->cfg->C("sign_type"));
		$this->reqHandler->createSign();
		$data = Utils::toXml($this->reqHandler->getAllParameters());
		$this->pay->setReqContent($this->reqHandler->getGateURL(), $data);
		if ($this->pay->call()) {
			$this->resHandler->setContent($this->pay->getResContent());
			$this->resHandler->setKey($this->reqHandler->getKey());
			if ($this->resHandler->isTenpaySign()) {
				$res = $this->resHandler->getAllParameters();
				Utils::dataRecodes("查询订单", $res);
				echo json_encode(["status" => 200, "msg" => "查询订单成功，请查看result.txt文件！", "data" => $res]);
				exit;
			}
			echo json_encode(["status" => 500, "msg" => "Error Code:" . $this->resHandler->getParameter("status") . " Error Message:" . $this->resHandler->getParameter("message")]);
		} else {
			echo json_encode(["status" => 500, "msg" => "Response Code:" . $this->pay->getResponseCode() . " Error Info:" . $this->pay->getErrInfo()]);
		}
	}
	public function closeOrder()
	{
		$this->reqHandler->setReqParams($_POST, ["method"]);
		$reqParam = $this->reqHandler->getAllParameters();
		if (empty($reqParam["out_trade_no"])) {
			echo json_encode(["status" => 500, "msg" => "请输入商户订单号!"]);
			exit;
		}
		$this->reqHandler->setParameter("version", $this->cfg->C("version"));
		$this->reqHandler->setParameter("service", "unified.trade.close");
		$this->reqHandler->setParameter("mch_id", $this->cfg->C("mchId"));
		$this->reqHandler->setParameter("nonce_str", mt_rand());
		$this->reqHandler->setParameter("sign_type", $this->cfg->C("sign_type"));
		$this->reqHandler->createSign();
		$data = Utils::toXml($this->reqHandler->getAllParameters());
		$this->pay->setReqContent($this->reqHandler->getGateURL(), $data);
		if ($this->pay->call()) {
			$this->resHandler->setContent($this->pay->getResContent());
			$this->resHandler->setKey($this->reqHandler->getKey());
			if ($this->resHandler->isTenpaySign()) {
				if ($this->resHandler->getParameter("status") == 0 && $this->resHandler->getParameter("result_code") == 0) {
					$res = $this->resHandler->getAllParameters();
					Utils::dataRecodes("关闭订单", $res);
					echo json_encode(["status" => 200, "msg" => "关闭订单成功,请查看result.txt文件！", "data" => $res]);
					exit;
				} else {
					echo json_encode(["status" => 500, "msg" => "Error Code:" . $this->resHandler->getParameter("err_code") . " Error Message:" . $this->resHandler->getParameter("err_msg")]);
					exit;
				}
			}
			echo json_encode(["status" => 500, "msg" => "Error Code:" . $this->resHandler->getParameter("status") . " Error Message:" . $this->resHandler->getParameter("message")]);
		} else {
			echo json_encode(["status" => 500, "msg" => "Response Code:" . $this->pay->getResponseCode() . " Error Info:" . $this->pay->getErrInfo()]);
		}
	}
	public function submitRefund()
	{
		$this->reqHandler->setReqParams($_POST, ["method"]);
		$reqParam = $this->reqHandler->getAllParameters();
		if (empty($reqParam["transaction_id"]) && empty($reqParam["out_trade_no"])) {
			echo json_encode(["status" => 500, "msg" => "请输入商户订单号或平台订单号!"]);
			exit;
		}
		$this->reqHandler->setParameter("version", $this->cfg->C("version"));
		$this->reqHandler->setParameter("service", "unified.trade.refund");
		$this->reqHandler->setParameter("mch_id", $this->cfg->C("mchId"));
		$this->reqHandler->setParameter("nonce_str", mt_rand());
		$this->reqHandler->setParameter("op_user_id", $this->cfg->C("mchId"));
		$this->reqHandler->setParameter("sign_type", $this->cfg->C("sign_type"));
		$this->reqHandler->createSign();
		$data = Utils::toXml($this->reqHandler->getAllParameters());
		$this->pay->setReqContent($this->reqHandler->getGateURL(), $data);
		if ($this->pay->call()) {
			$this->resHandler->setContent($this->pay->getResContent());
			$this->resHandler->setKey($this->reqHandler->getKey());
			if ($this->resHandler->isTenpaySign()) {
				if ($this->resHandler->getParameter("status") == 0 && $this->resHandler->getParameter("result_code") == 0) {
					$res = $this->resHandler->getAllParameters();
					Utils::dataRecodes("提交退款", $res);
					echo json_encode(["status" => 200, "msg" => "退款成功,请查看result.txt文件！", "data" => $res]);
					exit;
				} else {
					echo json_encode(["status" => 500, "msg" => "Error Code:" . $this->resHandler->getParameter("err_code") . " Error Message:" . $this->resHandler->getParameter("err_msg")]);
					exit;
				}
			}
			echo json_encode(["status" => 500, "msg" => "Error Code:" . $this->resHandler->getParameter("status") . " Error Message:" . $this->resHandler->getParameter("message")]);
		} else {
			echo json_encode(["status" => 500, "msg" => "Response Code:" . $this->pay->getResponseCode() . " Error Info:" . $this->pay->getErrInfo()]);
		}
	}
	public function queryRefund()
	{
		$this->reqHandler->setReqParams($_POST, ["method"]);
		if (count($this->reqHandler->getAllParameters()) === 0) {
			echo json_encode(["status" => 500, "msg" => "请输入商户订单号,平台订单号,商户退款单号,平台退款单号!"]);
			exit;
		}
		$this->reqHandler->setParameter("version", $this->cfg->C("version"));
		$this->reqHandler->setParameter("service", "unified.trade.refundquery");
		$this->reqHandler->setParameter("mch_id", $this->cfg->C("mchId"));
		$this->reqHandler->setParameter("nonce_str", mt_rand());
		$this->reqHandler->setParameter("sign_type", $this->cfg->C("sign_type"));
		$this->reqHandler->createSign();
		$data = Utils::toXml($this->reqHandler->getAllParameters());
		$this->pay->setReqContent($this->reqHandler->getGateURL(), $data);
		if ($this->pay->call()) {
			$this->resHandler->setContent($this->pay->getResContent());
			$this->resHandler->setKey($this->reqHandler->getKey());
			if ($this->resHandler->isTenpaySign()) {
				if ($this->resHandler->getParameter("status") == 0 && $this->resHandler->getParameter("result_code") == 0) {
					$res = $this->resHandler->getAllParameters();
					Utils::dataRecodes("查询退款", $res);
					echo json_encode(["status" => 200, "msg" => "查询成功,请查看result.txt文件！", "data" => $res]);
					exit;
				} else {
					echo json_encode(["status" => 500, "msg" => "Error Code:" . $this->resHandler->getParameter("err_code")]);
					exit;
				}
			}
			echo json_encode(["status" => 500, "msg" => $this->resHandler->getContent()]);
		} else {
			echo json_encode(["status" => 500, "msg" => "Response Code:" . $this->pay->getResponseCode() . " Error Info:" . $this->pay->getErrInfo()]);
		}
	}
	public function callback()
	{
		$xml = file_get_contents("php://input");
		$this->resHandler->setContent($xml);
		$this->resHandler->setKey($this->cfg->C("key"));
		if ($this->resHandler->isTenpaySign()) {
			if ($this->resHandler->getParameter("status") == 0 && $this->resHandler->getParameter("result_code") == 0) {
				$tradeno = $this->resHandler->getParameter("out_trade_no");
				return $this->resHandler->getAllParameters();
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
}